package api

import (
	"TaskAssignmentSystem/internal/app/services"
	"TaskAssignmentSystem/internal/pkg/middleware/handler"
	"TaskAssignmentSystem/pkg/utils"
	"github.com/kataras/iris/v12"
)

type LoginRequest struct {
	UserName string `json:"user_name" validate:"required"`
	PassWord string `json:"pass_word" validate:"required"`
}

// @Summary 登录
// @Description 登录
// @Tags 登录
// @Accept application/json
// @Produce application/json
// @param Accept-Version header string true "1.0.0"
// @Param param body LoginRequest true "登录参数"
// @Success 200 {object} utils.Response --> 成功后返回数据结构
// @Failure 400 {object} utils.ResponseError --> 失败后返回数据结构
// @Router /app/login [post]
func LoginHandler(ctx iris.Context) {
	var RequestParams LoginRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}
	var userServe services.SystemUserService
	if response, err := userServe.Login(RequestParams.UserName, RequestParams.PassWord); err != nil {
		ctx.JSON(utils.ResponseError{Code: iris.StatusInternalServerError, Message: err.Error()})
		return
	} else {
		ctx.JSON(utils.Response{Code: 200, Message: "ok", Data: response})
		return
	}
}

func LogoutHandler(ctx iris.Context) {
	var claims services.CustomClaims
	if err := claims.ParseClaims(ctx); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	}
	var userServe services.SystemUserService
	if err := userServe.Logout(claims.Id); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseOK())
		return
	}
}

type EditUserSelfPassWordRequest struct {
	OlePassWord string `json:"ole_pass_word" validate:"required"`
	NewPassWord string `json:"new_pass_word" validate:"required"`
}

// @Summary 更新管理员密码
// @Description 管理员更新自己的密码
// @Tags 用户
// @Accept application/json
// @Produce application/json
// @param Accept-Version header string true "1.0.0"
// @param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param param body EditUserSelfPassWordRequest true "更新管理员密码参数"
// @Success 200 {object} utils.Response --> 成功后返回数据结构
// @Failure 400 {object} utils.ResponseError --> 失败后返回数据结构
// @Router /app/user/change_password [post]
func UserEditPassWordHandler(ctx iris.Context) {
	var RequestParams EditUserSelfPassWordRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}
	var claims services.CustomClaims
	if err := claims.ParseClaims(ctx); err != nil {
		ctx.JSON(utils.ResponseError{Code: iris.StatusInternalServerError, Message: err.Error()})
		return
	}
	var systemAdminServe services.SystemUserService
	if err := systemAdminServe.UpdatePassWord(claims.Id, RequestParams.OlePassWord, map[string]interface{}{"pass_word": utils.Base64Encode(RequestParams.NewPassWord)}); err != nil {
		ctx.JSON(utils.ResponseError{Code: iris.StatusInternalServerError, Message: err.Error()})
		return
	} else {

		ctx.JSON(utils.Response{Code: 200, Message: "ok", Data: "ok"})
		return
	}
}

func AuthenticatedHandler(ctx iris.Context) {
	var i services.AuthServiceInterface
	i = &services.CustomClaims{AppSecret: []byte(utils.ServerConf.JwtConf.AppSecret)}
	if err := i.VerificationToken(ctx); err != nil {
		return
	}
	ctx.Next()
}

func UserAgreementHandler(ctx iris.Context) {
	var dictServe services.SystemDictService
	if data, err := dictServe.GetUserAgreement(); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseData(data))
		return
	}
}

func PrivacyAgreementHandler(ctx iris.Context) {
	var dictServe services.SystemDictService
	if data, err := dictServe.GetPrivacyAgreement(); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseData(data))
		return
	}
}

type RegisterRequest struct {
	Email    string `json:"email" validate:"required"`
	PassWord string `json:"pass_word" validate:"required"`
	Code     string `json:"code" validate:"required"`
	Team     string `json:"team"`
}

func RegisterHandler(ctx iris.Context) {
	var RequestParams RegisterRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}
	var userServe services.SystemUserService
	if data, err := userServe.Register(RequestParams.Email, RequestParams.PassWord, RequestParams.Code, RequestParams.Team); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseData(data))
		return
	}
}

type RegisterPhoneRequest struct {
	Phone    string `json:"phone" validate:"required"`
	PassWord string `json:"pass_word" validate:"required"`
	Code     string `json:"code" validate:"required"`
	Team     string `json:"team"`
}

func RegisterPhoneHandler(ctx iris.Context) {
	var RequestParams RegisterPhoneRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}
	var userServe services.SystemUserService
	if data, err := userServe.RegisterPhone(RequestParams.Phone, RequestParams.PassWord, RequestParams.Code, RequestParams.Team); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseData(data))
		return
	}
}

type ResetPwdEmailRequest struct {
	Email    string `json:"email" validate:"required"`
	PassWord string `json:"pass_word"`
	Code     string `json:"code" validate:"required"`
}

func ResetPwdEmailConfirmCodeHandler(ctx iris.Context) {
	var RequestParams ResetPwdEmailRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}
	var userServe services.SystemUserService
	if err := userServe.ResetPwdEmailConfirmCode(RequestParams.Email, RequestParams.Code); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseOK())
		return
	}
}

func ResetPwdEmailHandler(ctx iris.Context) {
	var RequestParams ResetPwdEmailRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}
	var userServe services.SystemUserService
	if err := userServe.ResetPwdEmail(RequestParams.Email, RequestParams.PassWord, RequestParams.Code); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseOK())
		return
	}
}

type ResetPwdPhoneRequest struct {
	Phone    string `json:"phone" validate:"required"`
	PassWord string `json:"pass_word"`
	Code     string `json:"code" validate:"required"`
}

func ResetPwdPhoneConfirmCodeHandler(ctx iris.Context) {
	var RequestParams ResetPwdPhoneRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}
	var userServe services.SystemUserService
	if err := userServe.ResetPwdPhoneConfirmCode(RequestParams.Phone, RequestParams.Code); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseOK())
		return
	}
}

func ResetPwdPhoneHandler(ctx iris.Context) {
	var RequestParams ResetPwdPhoneRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}
	var userServe services.SystemUserService
	if err := userServe.ResetPwdPhone(RequestParams.Phone, RequestParams.PassWord, RequestParams.Code); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseOK())
		return
	}
}
